#pragma once
//#include <d3dx9math.h>
#include "..//Math//Tuple2.h"
#include "..//Math//Tuple3.h"
#include <vector>
#include <string>

//#define CONVERTE_ENABLE

class CModelObjectData
{
public:
	unsigned int VertexIndexCount;
	unsigned int VertexCount;
	unsigned int NormalsCount;
	unsigned int UVsCount;
	//unsigned int TexturesCount;
	
	int			MaterialIndex;

	int* pVertexIndexBuffer;
	Tuple3f* pVertexBuffer;
	Tuple3f* pNormalsBuffer;
	Tuple2f* pUVsBuffer;

	//std::string*		 pTextureFileNames;

	CModelObjectData()
	{
		VertexIndexCount = 0;
		VertexCount = 0;
		NormalsCount = 0;
		UVsCount = 0;
		//TexturesCount = 0;

		MaterialIndex = -1;

		pVertexIndexBuffer = NULL;
		pVertexBuffer = NULL;
		pNormalsBuffer = NULL;
		pUVsBuffer = NULL;
		//pTextureFileNames = NULL;
	}

	void Reset()
	{
		if(pVertexIndexBuffer)
		{
			free(pVertexIndexBuffer);
			//delete[](pVertexIndexBuffer);
			pVertexIndexBuffer = NULL;
			VertexIndexCount = 0;
		}

		if(pVertexBuffer)
		{
			free(pVertexBuffer);
			//delete[](pVertexBuffer);
			pVertexBuffer = NULL;
			VertexCount = 0;
		}

		if(pNormalsBuffer)
		{
			free(pNormalsBuffer);
			//delete[](pNormalsBuffer);
			pNormalsBuffer = NULL;
			NormalsCount = 0;
		}

		if(pUVsBuffer)
		{
			free(pUVsBuffer);
			//delete[](pUVsBuffer);
			pUVsBuffer = NULL;
			UVsCount = 0;
		}

		MaterialIndex = -1;
		/*if(pTextureFileNames)
		{
			delete[](pTextureFileNames);
			pTextureFileNames = NULL;
			pTextureFileNames = 0;
		}*/
	}

	void ResizeBuffers(int NewVertexIndexCount
					 , int NewVertexCount
					 , int NewNormalsCount
					 , int NewUVsCount
					/* , int NewTexturesCount*/)
	{
		Reset();
		if(NewVertexIndexCount>0)
		{
			pVertexIndexBuffer = (int*) malloc(sizeof(int)*NewVertexIndexCount);
			//pVertexIndexBuffer = (int *)new int [NewVertexIndexCount];
			VertexIndexCount = NewVertexIndexCount;
		}
		
		if(NewVertexCount>0)
		{
			pVertexBuffer = (Tuple3f*) malloc(sizeof(Tuple3f)*NewVertexCount);
			//pVertexBuffer = (Tuple3f *)new Tuple3f [NewVertexCount];
			VertexCount = NewVertexCount;
		}

		if(NewNormalsCount>0)
		{
			pNormalsBuffer = (Tuple3f*) malloc(sizeof(Tuple3f)*NewNormalsCount);
			//pNormalsBuffer = (Tuple3f *)new Tuple3f [NewNormalsCount];
			NormalsCount = NewNormalsCount;
		}

		if(NewUVsCount>0)
		{
			pUVsBuffer = (Tuple2f*) malloc(sizeof(Tuple2f)*NewUVsCount);
			//pUVsBuffer = (Tuple2f *)new Tuple2f [NewUVsCount];
			UVsCount = NewUVsCount;
		}

		MaterialIndex = -1;

		//if(NewTexturesCount>0)
		//{
		//	pTextureFileNames = (std::string *)new std::string [NewTexturesCount];
		//	TexturesCount = NewTexturesCount;
		//}
	}

};

class CStaticMeshData
{
public:
	std::vector<CModelObjectData>	ModelObjects;
	std::vector<std::string>				TextureList;
public:

#ifdef CONVERTE_ENABLE
	bool							CreateFrom3DSData(void* pData);
	bool							DumpToFile(std::string fileName);
#endif
	void							Render();

	bool							CreateFromFile(std::string fileName);
	void							Reset();
	CStaticMeshData(void);
	~CStaticMeshData(void);
};
